package com.lin.sms.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lin.sms.domain.Score;
import com.lin.sms.domain.User;
import com.lin.sms.page.PageResult;
import com.lin.sms.service.ScoreService;
import com.lin.sms.service.TxService;
import com.lin.sms.util.Constant;

@Controller
@RequestMapping("/scores")
public class ScoreController {

    @Autowired
    TxService txService;
    
   @Autowired
    ScoreService scoreService;

    // 返回欢迎界面
    @RequestMapping("/welcome")
    public String welcome() {
        return "forward:/WEB-INF/views/welcome.jsp";
    }

    @RequestMapping("/txTest")
    public String txTest() {
        Score score = new Score();
        score.setName("jjlin");
        score.setClassId("1");
        score.setChineseScore(100);
        score.setEnglishScore(99);
        score.setMathScore(99);
        
      //  txService.saveScore(score);
        
        return "forward:/WEB-INF/views/welcome.jsp";
        
        
    }
    /**
     * 通过分页显示list
     */
    @RequestMapping("/listByPage")
    public String listScoresByPage(HttpServletRequest req, Integer currentPage,  Integer pageSize) {
      // pageSize = (Integer)req.getSession().getAttribute("pageSize");
        if(currentPage == null) {//默认第一次请求当前页为1
            currentPage = 1;
        }
        if(pageSize == null) {
            pageSize = (Integer)req.getSession().getAttribute("pageSize");
            if(pageSize == null) {
                pageSize = 20;//默认一页显示20条数据
            }
        } else {
            req.getSession().setAttribute("pageSize", pageSize);
        }
        
        String option = (String)req.getSession().getAttribute(Constant.OPTION_IN_SESSION);
        User user = (User)req.getSession().getAttribute(Constant.USER_IN_SESSION);
        if (option == null || option.equals("")) {
            return "forward:WEB-INF/views/welcome.jsp";
        }

        char value = option.charAt(0);
        switch (value) {
            case '1':
                System.out.println(currentPage);
                
                PageResult pageResult = scoreService.queryPage(user, currentPage, pageSize);
                if (currentPage > pageResult.getTotalPage()) {
                    currentPage = pageResult.getTotalPage();
                    pageResult = scoreService.queryPage(user, currentPage, pageSize);
                }
                System.out.println(pageResult.getCurrentPage());
                req.setAttribute("pageResult", pageResult);
                return "forward:/WEB-INF/views/scores/listByPage.jsp";
            case '2':
                
        }
        return null;
    }
    
    /**
     * list 
     */
    @RequestMapping("/list")
    public String listScores(HttpServletRequest req, HttpServletResponse resp, RedirectAttributes redirectAttributes,  String option) throws Exception {
        User user = (User)req.getSession().getAttribute(Constant.USER_IN_SESSION);
        if (option == null || option.equals("")) {
            return "forward:/WEB-INF/views/welcome.jsp";
        }
        //把option丢到Session中去,跳转到/listByPage需要用到
        req.getSession().setAttribute(Constant.OPTION_IN_SESSION, option);
        // 定义一个list存放查询的scores
       // Integer currentPage = 1;//默认设置当前页为1
      //  Integer pageSize = 20;//默认设置一页20条数据
        char value = option.charAt(0);

        switch (value) {
            case '1':
//                pageResult = scoreService.queryScoresResult(user, currentPage, pageSize);
//                req.setAttribute(Constant.SCORES, scores);
//                req.setAttribute("currentPage", currentPage);
//                req.getRequestDispatcher("/scores/listByPage").forward(req, resp);
//                resp.sendRedirect("/s/scores/listByPage");
//                return "redirect:/scores/listByPage";
//                redirectAttributes.addFlashAttribute("currentPage", currentPage);
//                redirectAttributes.addFlashAttribute("pageSize", pageSize);
//                req.setAttribute("currentPage", currentPage);
//                req.setAttribute("pageSize", pageSize);
                return "redirect:/scores/listByPage";
            case '2':
                return "redirect:/scores/listByPage";
            case '3':
                return "redirect:/scores/listByPage";
            case '4':
                return "redirect:/scores/listByPage";
            case '5':
                double englishAverage = scoreService.qureyEnglishAverage(user);
                req.setAttribute("value", englishAverage);
                return "forward:/WEB-INF/views/scores/listOne.jsp";
            case '6':
                int count = scoreService.qureyEnglishMoreThan90(user);
                req.setAttribute("value", count);
                return "forward:/WEB-INF/views/scores/listOne.jsp";
            case '7':
                return "forward:/WEB-INF/views/scores/input.jsp";
        }
        return null;
    }

    @RequestMapping("/queryOne")
    public String queryOne(HttpServletRequest req, String studentName) {
        User user = (User)req.getSession().getAttribute(Constant.USER_IN_SESSION);
        if (user == null) {
            return "redirect:/login.jsp";
        }
        List<Score> scores = scoreService.queryOne(user, studentName);
        req.getSession().setAttribute("scores", scores);
        return "forward:/WEB-INF/views/scores/list.jsp";
    }

}
